﻿<UserControl x:Class="SLExtensions.Showcase.PageResourceSelector"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ctl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    xmlns:sleData="clr-namespace:SLExtensions.Data;assembly=SLExtensions.Data"
    xmlns:local="clr-namespace:SLExtensions.Showcase">
    <UserControl.Resources>

        <sleData:ResourceSelector x:Key="personTemplateSelector">
            <ResourceDictionary>
                <DataTemplate x:Key="Baby">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding FirstName}" />
                        <TextBlock Text=" is a baby" />
                    </StackPanel>
                </DataTemplate>
                <DataTemplate x:Key="Child">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding FirstName}" />
                        <TextBlock Text=" is a child" />
                    </StackPanel>
                </DataTemplate>
                <DataTemplate x:Key="Teenager">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding FirstName}" />
                        <TextBlock Text=" is a teenager" />
                    </StackPanel>
                </DataTemplate>
                <DataTemplate x:Key="Adult">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding FirstName}" />
                        <TextBlock Text=" is an adult" />
                    </StackPanel>
                </DataTemplate>
            </ResourceDictionary>
        </sleData:ResourceSelector>

        <sleData:ResourceSelector x:Key="myTemplateSelector">
            <ResourceDictionary>
                <DataTemplate x:Key="TypeA">
                    <TextBlock Text="This is the template for Type A" Foreground="Blue" />
                </DataTemplate>
                
                <DataTemplate x:Key="TypeB">
                    <TextBlock Text="This is the template for Type B" Foreground="Red" />
                </DataTemplate>
            </ResourceDictionary>
        </sleData:ResourceSelector>
        
        
    </UserControl.Resources>

    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="myItemTemplate">
                <ContentControl Content="{Binding}"
                            ContentTemplate="{Binding Converter={StaticResource myTemplateSelector}}"/>
            </DataTemplate>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        
        <ItemsControl x:Name="dynTplItemsControl" ItemTemplate="{StaticResource myItemTemplate}">
        </ItemsControl>

        <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
            <Button Margin="2" Content="Add item of TypeA" Click="OnAddTypeA" />
            <Button Margin="2" Content="Add item of TypeB" Click="OnAddTypeB"/>
        </StackPanel>

        <ItemsControl Grid.Column="1">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ContentControl Content="{Binding}"
                                        ContentTemplate="{Binding Converter={StaticResource personTemplateSelector}}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <local:Person FirstName="Arthur" Age="1" />
            <local:Person FirstName="Jade" Age="4" />
            <local:Person FirstName="Aurore" Age="15" />
            <local:Person FirstName="Simon" Age="23" />
        </ItemsControl>
    </Grid>
       
</UserControl>
